﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
using UnityEngine;

public class GameTool
{
    public static int ScreenWidth
    {
        get
        {
            UIRoot root = AllUpdate.UIRoot.GetComponent<UIRoot>();
            float s = (float)root.activeHeight / Screen.height;
            return Mathf.CeilToInt(Screen.width * s);
        }
    }

    public static int ScreenHeight
    {
        get
        {
            UIRoot root = AllUpdate.UIRoot.GetComponent<UIRoot>();
            float s = (float)root.activeHeight / Screen.height;
            return Mathf.CeilToInt(Screen.height * s);
        }
    }

    /// <summary>
    /// 获取一个对象的在舞台上的路径到根节点为止； 
    /// </summary>
    /// <param name="t"></param>
    public static string GetTransformPatch(Transform t)
    {
        string url = NGUITools.GetHierarchy(t.gameObject);
        return url.Replace('\\', '/');
    }

    public static void GetChildOfTag(Transform t, ref List<Transform> list, string tag, bool includeSelf = false)
    {
        if (t == null) { return; }
        if (includeSelf)
        {
            if (t.gameObject.CompareTag(tag))
            {
                list.Add(t);
            }
        }
        for (int idx = 0; idx < t.childCount; ++idx)
        {
            Transform trans = t.GetChild(idx);
            if (trans.gameObject.CompareTag(tag))
            {
                list.Add(trans);
            }
            GetChildOfTag(trans, ref list, tag);
        }
    }

    /// <summary>
    /// 世界坐标转成ui坐标
    /// </summary>
    public static Vector3 PositionToUI(Vector3 pos)
    {
        UIRoot root = AllUpdate.UIRoot.GetComponent<UIRoot>();
        Vector3 scale = root.transform.lossyScale;
        Vector3 rPos = root.transform.localPosition;
        return new Vector3((pos.x - rPos.x) / scale.x,
        (pos.y - rPos.y) / scale.y, (pos.z - rPos.z) / scale.z);
    }

    /// <summary>
    /// 屏幕坐标转成ui坐标
    /// </summary>
    /// <param name="vector2"></param>
    public static Vector3 PointToUI(Vector2 vector2)
    {
        UIRoot root = AllUpdate.UIRoot.GetComponent<UIRoot>();
        float s = (float)root.activeHeight / Screen.height;
        float x = vector2.x * s - ScreenWidth * 0.5f;
        float y = vector2.y * s - ScreenHeight * 0.5f;
        return new Vector3(x, y);
    }

    /// <summary>
    /// 替换字符串
    /// </summary>
    /// <param name="info"></param>
    /// <param name="args"></param>
    public static string ReplaceString(string info, params string[] args)
    {
        if (args.Length <= 0) { return info; }
        for (int i = 0; i < args.Length; i++)
        {
            info = StringTool.EReplace(info, "{D}", args[i]);
        }
        return info;
    }

    public static string NameToView(string name)
    {
        if (name.Length > 6)
        {
            char[] charArr = name.ToCharArray();
            int size = Encoding.Default.GetBytes(charArr, 0, 6).Length;
            if (size >= 12)
            {
                return name.Substring(0, 6) + "...";
            }
            for (int i = 6; i < charArr.Length; i++)
            {
                size += Encoding.Default.GetBytes(charArr, i, 1).Length;
                if (size == 12)
                {
                    if (i + 1 < charArr.Length)
                    {
                        return name.Substring(0, i + 1) + "...";
                    }
                    return name.Substring(0, i + 1);
                }
                else if (size > 12)
                {
                    return name.Substring(0, i) + "...";
                }
            }
        }
        return name;
    }

    public static int GetStringLength(string str)
    {
        if (str.Length == 0) { return 0; }
        ASCIIEncoding encoding = new ASCIIEncoding();
        int tempLen = 0; byte[] bytes = encoding.GetBytes(str);
        for (int i = 0; i < bytes.Length; i++)
        {
            if (bytes[i] == 63)
            {
                tempLen += 2;
            }
            else
            {
                tempLen += 1;
            }
        }
        return tempLen;
    }
    
    public static string GetEnumString(Enum value)
    {
        FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
        object[] args = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
        DescriptionAttribute[] attributes = (DescriptionAttribute[])args;
        if (attributes.Length > 0)
        {
            return attributes[0].Description;
        }
        else
        {
            return value.ToString();
        }
    }

    public static ArrayList GetFile(string name)
    {
        string path = Application.persistentDataPath + "//" + name;
        ArrayList strlist = new ArrayList(); StreamReader str;
        try
        {
            str = File.OpenText(path);
        }
        catch (Exception)
        {
            return strlist;
        }
        while (!string.IsNullOrEmpty(str.ReadLine()))
        {
            strlist.Add(str.ReadLine());
        }
        str.Close(); str.Dispose();
        return strlist;
    }

    public static void CreatFile(string name, string nInfo)
    {
        string path = Application.persistentDataPath + "//" + name;
        FileInfo file = new FileInfo(path); StreamWriter stw;
        if (!file.Exists)
        {
            stw = file.CreateText();
        }
        else
        {
            stw = file.AppendText();
        }
        stw.WriteLine(nInfo);
        stw.Close(); stw.Dispose();
    }
}